﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FireZone.NativeWMDMInteface;
using FireZone.OperationLogger;
using System.Runtime.InteropServices;

namespace FireZone.WMDMHelper
{
    internal class WMDMProgressImplHelper : IWMDMProgress3
    {
        private OperationLoggerItem _loggerItem;
        private int _estimatedTicks;

        public WMDMProgressImplHelper(OperationLoggerItem loggeritem)
        {
            _loggerItem = loggeritem;
            _estimatedTicks = 0;
        }

        #region IWMDMProgress3 Members

        public void Begin(int dwEstimatedTicks)
        {
            throw new NotImplementedException();
        }

        public void Progress(int dwTranspiredTicks)
        {
            throw new NotImplementedException();
        }

        public void End()
        {
            throw new NotImplementedException();
        }

        public void End2(int hrCompletionCode)
        {
            throw new NotImplementedException();
        }

        public void Begin3(Guid EventId, int dwEstimatedTicks, IntPtr pContext)
        {
            _loggerItem.DisplayMessage = "Begin transfer";
            _estimatedTicks = dwEstimatedTicks;
            _loggerItem.HasProgress = true;
            //throw new NotImplementedException();            
        }

        public void Progress3(Guid EventId, int dwTranspiredTicks, IntPtr pContext)
        {
            //throw new NotImplementedException();
            if(EventId == WMDM_PROGRSS_EVENT_ID.EVENT_WMDM_CONTENT_TRANSFER)
            {
                _loggerItem.ProgressPercent = dwTranspiredTicks * 100 / _estimatedTicks;
            }
            else if(EventId == WMDM_PROGRSS_EVENT_ID.SCP_EVENTID_ACQSECURECLOCK)
            {

            }
            else if(EventId == WMDM_PROGRSS_EVENT_ID.SCP_EVENTID_DRMINFO)
            {

            }
            else if(EventId == WMDM_PROGRSS_EVENT_ID.SCP_EVENTID_NEEDTOINDIV)
            {

            }
            else if(EventId == WMDM_PROGRSS_EVENT_ID.SCP_PARAMID_DRMVERSION)
            {

            }
        }

        public void End3(Guid EventId, int hrCompletionCode, IntPtr pContext)
        {
            if(hrCompletionCode<0)
            {
                _loggerItem.DisplayMessage = Marshal.GetExceptionForHR(hrCompletionCode).Message;
            }
            else
            {
                _loggerItem.DisplayMessage = "transfer OK";
            }
            _loggerItem.HasProgress = false;
            
            //throw new NotImplementedException();
        }

        #endregion
    }
}
